﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Budget
{
    public partial class CategoryEdit : Form
    {
        public CategoryEdit()
        {
            InitializeComponent();
            updateGridView();
        }
        private int exp_initialrowcount;
        private int inc_initialrowcount;
        private DataTable dt_expense_categories = new DataTable();
        private DataTable dt_income_categories = new DataTable();
        private void CategoryEdit_Load(object sender, EventArgs e)
        {
            try
            {
                //// TODO: This line of code loads data into the 'budgetDataSet1.getIncomeCategories' table. You can move, or remove it, as needed.
                //this.getIncomeCategoriesTableAdapter.Fill(this.budgetDataSet.getIncomeCategories);

                //// TODO: This line of code loads data into the 'budgetDataSet.tblCategoryExpense' table. You can move, or remove it, as needed.
                //this.tblCategoryExpenseTableAdapter.FillExpenseDataTable(this.budgetDataSet.tblCategoryExpense);
                
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void updateGridView()
        {
            dt_income_categories.Clear();
            dt_expense_categories.Clear();

            dt_expense_categories = DatabaseQuery.ExecStringProc(StoredProcs.GetTransactionCategories(Money.TransactionType.Expense));
            dt_income_categories = DatabaseQuery.ExecStringProc(StoredProcs.GetTransactionCategories(Money.TransactionType.Income));
            foreach(DataRow dr in dt_expense_categories.Rows)
            {
                dataGridView1.Rows.Add(dr["ID"].ToString(),dr["Category"].ToString(),dr["Hidden"].ToString(),dr["BudgetAmount"].ToString(),dr["Fixed"].ToString(),dr["Luxury"].ToString());
            }
            foreach(DataRow dr in dt_income_categories.Rows)
            {
                dataGridView2.Rows.Add(dr["ID"].ToString(),dr["Category"].ToString());
            }
            exp_initialrowcount = dataGridView1.Rows.Count;
            inc_initialrowcount = dataGridView2.Rows.Count;
        }


        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= exp_initialrowcount)
            {
                //this.tblCategoryExpenseTableAdapter.Insert(dataGridView1.Rows[e.RowIndex].Cells["categoryDataGridViewTextBoxColumn"].Value.ToString());
                //updateGridView();
                //exp_initialrowcount++;
            }
            else
            {
                //this.tblCategoryExpenseTableAdapter.Update(dataGridView1.Rows[e.RowIndex].Cells["categoryDataGridViewTextBoxColumn"].Value.ToString(), Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["iDDataGridViewTextBoxColumn"].Value));
            }

        }

        private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= inc_initialrowcount)
            {
                //this.getIncomeCategoriesTableAdapter.Insert(dataGridView2.Rows[e.RowIndex].Cells["CategorydataGridView"].Value.ToString());
                //updateGridView();
                //inc_initialrowcount++;
            }
            else
            {
                //this.getIncomeCategoriesTableAdapter.Update(dataGridView2.Rows[e.RowIndex].Cells["CategorydataGridView"].Value.ToString(), Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["iDDataGridViewTextBoxColumn"].Value));
            }
        }

    }
}
